<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\model\Classes;
use app\common\model\CourseDetail;
use app\common\model\Teach;
use function Couchbase\defaultDecoder;

/**
 * 学生
 *
 * Class Teacher
 * User zhenzihan
 * Date 2020/6/16
 * @package app\api\controller\v1
 */
class Student extends Api
{
    /**
     * 课程表
     *
     * @User zhenzihan
     * @Date 2020/6/16
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function course()
    {
        $student_id = $this->request->request('id');
//        $semester_id = $this->request->request('semester_id', 0);
//        $semester_num = $this->request->request('semester_num', 0);

        $student = \app\common\model\Student::get($student_id);
        if (!$student) {
            $this->error('学生不存在');
        }

        $semester = \app\common\model\Semester::where('start_date', '<=', date('Y-m-d', time()))
            ->where('end_date', '>=', date('Y-m-d', time()))
            ->find();
        if (!$semester) {
            $this->error('未设置学期');
        }

        $start_date = strtotime($semester->start_date);
        $semester_num = floor(abs(($start_date - (strftime("%u", $start_date) - 1) * 86400) - (time() - (strftime("%u", time()) - 1) * 86400)) / (24 * 60 * 60 * 7)) + 1;

        // 课表
        $list = CourseDetail::where('class_id', $student->class_id)
            ->where('semester_id', $semester->id)
            ->where('semester_num', $semester_num)
            ->select()
            ->toArray();

        $teach_ids = array_unique(array_column($list, 'teach_id'));
        $class_ids = array_unique(array_column($list, 'class_id'));

        // 教师任课
        $teaches = [];
        if ($teach_ids) {
            $teaches = Teach::alias('teach')
                ->join('__SUBJECT__ subject', 'subject.id = teach.subject_id')
                ->join('__TEACHER__ teacher', 'teacher.id = teach.teacher_id')
                ->join('__USER__ user', 'user.id = teacher.user_id')
                ->whereIn('teach.id', $teach_ids)
                ->column('teach.id,teach.type,subject.name subject_name,user.nickname teacher_name', 'teach.id');
        }

        $num = 1;
        foreach ($teaches as $key => $value) {
            $teaches[$key]['num'] = $num > 8 ? $num % 8 : $num;
            $num++;
        }

        // 班级
        $classes = [];
        if ($class_ids) {
            $classes = Classes::alias('class')
                ->join('__GRADE__ grade', 'grade.id = class.grade_id')
                ->join('__CLASSROOM__ room', 'room.id = class.classroom_id')
                ->join('__BUILDING__ building', 'building.id = room.building_id')
                ->whereIn('class.id', $class_ids)
                ->column('class.id,class.name class_name,grade.name grade_name,building.name building_name,room.name classroom_name', 'class.id');
        }
        $course_details = [];
        foreach ($list as $key => $value) {
            if (isset($teaches[$value['teach_id']]) && isset($classes[$value['class_id']])) {
                $course_details[$value['day']][$value['number']] = [
                    'class_name' => $classes[$value['class_id']]['class_name'],
                    'grade_name' => $classes[$value['class_id']]['grade_name'],
                    'building_name' => $classes[$value['class_id']]['building_name'],
                    'classroom_name' => $classes[$value['class_id']]['classroom_name'],
                    'subject_name' => $teaches[$value['teach_id']]['subject_name'],
                    'teacher_name' => $teaches[$value['teach_id']]['teacher_name'],
                    'num' => $teaches[$value['teach_id']]['num'],
                ];
            }
        }

        //所有课节
        $list = \app\common\model\ClassDay::where('school_id', $student->school_id)->select();

        $data = [];
        $weeks = get_week_arr();
        foreach ($list as $key => $class_day) {

            if (isset($weeks[$class_day['day'] - 1]) && !isset($data[$class_day['day']])) {
                $data[$class_day['day']] = $weeks[$class_day['day'] - 1];
            }

            if (isset($course_details[$class_day['day']]) && isset($course_details[$class_day['day']][$class_day['number']])) {
                $temp = $course_details[$class_day['day']][$class_day['number']];
                $temp['start_time'] = $class_day['start_time_text'];
                $temp['end_time'] = $class_day['end_time_text'];
                $data[$class_day['day']]['list'][] = $temp;
            }
        }

        $this->success('', array_values($data));
    }
}
